/*
 * stream.c
 *
 *  Created on: 28th Dec 2010
 *      Author: bob
 */

#include "stream.h"

// PUBLIC
int lib_shout_init() {

    // Initialise the library
    shout_init();

    // Create a new connection
    shout_struct = shout_new();
    if (shout_struct == NULL){
        return -100;
    }
    return SHOUTERR_SUCCESS;
}

int lib_shout_open(char* samplerate){

    int r;
    if(shout_struct == NULL) return -100;
    if((r = lib_shout_populate(samplerate)) != SHOUTERR_SUCCESS) return r;
    return shout_open(shout_struct);
}

int lib_shout_close() {

    // Close the service
    if(shout_struct == NULL) return -1;
    shout_close(shout_struct);
    shout_free(shout_struct);
    shout_shutdown();
    return SHOUTERR_SUCCESS;
}

int lib_shout_send(char *data, int len) {

    // Send data to the channel
    if(shout_struct == NULL) return -100;
    shout_sync(shout_struct);
    //printf("Sending %d", len);
    return shout_send(shout_struct, (unsigned char*)data, (size_t)len);
}

// PRIVATE
int lib_shout_populate(char* samplerate) {

    // Set the initial prameters
    int r;
    if(shout_struct == NULL) return -100;
    if((r=shout_set_host(shout_struct, "127.0.0.1")) != SHOUTERR_SUCCESS) return r;
    if((r=shout_set_port(shout_struct, 9998)) != SHOUTERR_SUCCESS) return r;
    if((r=shout_set_user(shout_struct, "source")) != SHOUTERR_SUCCESS) return r;
    if((r=shout_set_password(shout_struct, "bob")) != SHOUTERR_SUCCESS) return r;
    if((r=shout_set_mount(shout_struct, "/home/bob/DcSdr/icecast")) != SHOUTERR_SUCCESS) return r;
    if((r=shout_set_format(shout_struct, SHOUT_FORMAT_VORBIS)) != SHOUTERR_SUCCESS) return r;
    if((r=shout_set_protocol(shout_struct, SHOUT_PROTOCOL_HTTP)) != SHOUTERR_SUCCESS) return r;
    if((r=shout_set_audio_info(shout_struct, SHOUT_AI_SAMPLERATE, samplerate)) != SHOUTERR_SUCCESS) return r;
    if((r=shout_set_audio_info(shout_struct, SHOUT_AI_BITRATE, "128")) != SHOUTERR_SUCCESS) return r;
    if((r=shout_set_audio_info(shout_struct, SHOUT_AI_CHANNELS, "2")) != SHOUTERR_SUCCESS) return r;
    return SHOUTERR_SUCCESS;
}
